package me.thinkjet.sso.client.validation;

import java.util.Map;

import me.thinkjet.sso.client.util.CommonUtils;
import me.thinkjet.sso.client.util.JSONUtils;

public class ServiceTicketValidator extends AbstractUrlBasedTicketValidator {

	public ServiceTicketValidator(final String validateUrl,
			final String clientId, final String key) {
		super(validateUrl, clientId, key);
	}

	protected final Assertion parseResponseFromServer(final String response)
			throws TicketValidationException {
		final String error = JSONUtils.getTextForElement(response,
				"authenticationFailure");

		if (CommonUtils.isNotBlank(error)) {
			throw new TicketValidationException(error);
		}

		final String id = JSONUtils.getTextForElement(response, "id");
		if (CommonUtils.isEmpty(id)) {
			throw new TicketValidationException(
					"No id was found in the response from the CAS server.");
		}

		final Assertion assertion;
		final Map<String, Object> attributes = JSONUtils.getTextForElements(
				response, "attributes");
		assertion = new AssertionImpl(id, attributes);
		return assertion;
	}

}
